package com.sgai.zero_robot.ui.control;


import com.sgai.zero_robot.database.ZeroDetectionEntity;
import com.sgai.zero_robot.database.ZeroDetectionRepository;
import com.sgai.zero_robot.pojo.ZeroListInfo;
import com.sgai.zero_robot.ui.history.RunnableCallBack;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LoadCurrentRunnable implements Runnable {
    private final RunnableCallBack<List<ZeroListInfo>> callBack;
    private final ZeroDetectionRepository repository;
    private final String lineName;
    private final String towerNum;
    private final int install;
    private final int phase;

    public LoadCurrentRunnable(ZeroDetectionRepository repository, RunnableCallBack<List<ZeroListInfo>> callBack,
                               String lineName, String towerNum, int install, int phase){
        this.callBack=callBack;
        this.repository=repository;
        this.lineName =lineName;
        this.towerNum=towerNum;
        this.install=install;
        this.phase=phase;
    }
    @Override
    public void run() {
        //左串
        List<ZeroDetectionEntity> list = repository.getByParams(lineName,towerNum,install,phase,1);
        //中串
        List<ZeroDetectionEntity> list1 = repository.getByParams(lineName,towerNum,install,phase,2);
        //右串
        List<ZeroDetectionEntity> list2 = repository.getByParams(lineName,towerNum,install,phase,3);
        List<ZeroListInfo> infos = mergeLists(list, list1, list2);
        if (callBack!=null){
            callBack.onResult(infos);
        }
    }
    public List<ZeroListInfo> mergeLists(List<ZeroDetectionEntity> leftList,
                                                List<ZeroDetectionEntity> midList,
                                                List<ZeroDetectionEntity> rightList) {
        // Map<position, ZeroListInfo>
        Map<Integer, ZeroListInfo> map = new HashMap<>();

        // 处理左串
        for (ZeroDetectionEntity entity : leftList) {
            ZeroListInfo info = map.computeIfAbsent(entity.getPosition(), p -> {
                ZeroListInfo z = new ZeroListInfo();
                z.setIndex(p);
                return z;
            });
            info.setLeft(entity);
        }

        // 处理中串
        for (ZeroDetectionEntity entity : midList) {
            ZeroListInfo info = map.computeIfAbsent(entity.getPosition(), p -> {
                ZeroListInfo z = new ZeroListInfo();
                z.setIndex(p);
                return z;
            });
            info.setMid(entity);
        }

        // 处理右串
        for (ZeroDetectionEntity entity : rightList) {
            ZeroListInfo info = map.computeIfAbsent(entity.getPosition(), p -> {
                ZeroListInfo z = new ZeroListInfo();
                z.setIndex(p);
                return z;
            });
            info.setRight(entity);
        }

        // 转成列表并按照 index 排序
        List<ZeroListInfo> result = new ArrayList<>(map.values());
        result.sort(Comparator.comparingInt(ZeroListInfo::getIndex));

        return result;
    }
}